package algorithm.t202111;

import java.util.Arrays;

/**
 * @author : 李红磊
 * @version :1.0
 * @date : 2021/11/18 9:14
 * @description :6道
 * 埃鲁德·基普乔格 Eliud Kipchoge
 * persevere to last
 * 2021.11.18
 * 2021年11月18日19:04:35
 * 李红磊
 */
public class t20211118 {

    //563.二叉树的坡度
    public int findTilt(TreeNode root) {
        if (root == null) return 0;
        int res = 0;
        dfs(root, res);
        return res;
    }

    private int dfs(TreeNode root, int res) {
        if (root == null) return 0;
        int left_value = dfs(root.left, res);
        int right_value = dfs(root.right, res);

        res = Math.abs(left_value - right_value);
        return left_value + right_value + root.val;
    }

    //剑指18.删除链表的节点
    public ListNode deleteNode(ListNode head, int val) {
        return null;
    }

    //剑指22.链表中倒数第k个节点
    public ListNode getKthFromEnd(ListNode head, int k) {
        return null;
    }

    //剑指25.合并两个有序链表
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1), cur = dummy;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;

        }
        cur.next = l1 == null ? l1 : l2;
        return dummy.next;
    }

    //剑指52.两个链表的第一个公共节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        return null;
    }

    //剑指21.调整数组顺序使奇数位于偶数前面
    public int[] exchange(int[] nums) {
        if(nums==null||nums.length<2)return nums;
        int len=nums.length;

        int left=0,right=len-1,mid=left+right>>>1;

        Arrays.sort(nums);
        while(left<right){
            while(left<mid&&nums[left]%2!=0){
                left++;
            }
            while(right>=mid&&nums[right]%2==0){
                right--;
            }


            int temp=nums[left];
            nums[left]=nums[right];
            nums[right]=temp;
            left++;
            right--;
        }
        return nums;
    }

    public static void main(String[] args) {
        t20211118 t20211118 = new t20211118();

        Arrays.stream(t20211118.exchange(new int[]{3,19,10,17,9,12,9,3,9,20})).forEach(s-> System.out.println(s));
        System.out.println("3,19,10,17,9,12,9,3,9,20");

    }

}
